<?php

namespace app\queue\stomp;

use app\queue\BaseConsumer;
use common\model\TaskModel;
use Webman\Stomp\Client;
use Webman\Stomp\RetryAbleConsumer;

/**
 * 任务返修去更新实体状态
 */
class TaskRepairUpdateEntityStatusConsumer extends BaseConsumer implements RetryAbleConsumer
{
    // 要消费的队列名
    public $queue = 'task_repair_update_entity_status';

    // 连接名，对应 config/redis_queue.php 里的连接`
    public $connection = 'default';

    /**
     * 消费
     * @param $data
     * @return void
     * @throws \Throwable
     */
    public function doConsume($data)
    {
        $taskIdList = $data['need_repair_task_id_list'];
        if (empty($taskIdList)) {
            return;
        }
        $entityIdList = model(TaskModel::class)->where(['id' => ['IN', implode(',', $taskIdList)]])->field('source_entity_id')->distinct(true)->column('source_entity_id');
        foreach ($entityIdList as $entityId) {
            Client::send('check_entity_status', build_queue_data_with_xu(['entity_id' => $entityId]), 1);
        }
    }

    /**
     * Summary of getTtr
     * @return int
     */
    public function getTtr()
    {
        return 20;
    }

    /**
     * Summary of canRetry
     * @param mixed $attempt
     * @param mixed $error
     * @return bool
     */
    public function canRetry($attempt, $error)
    {
        trace(format_exception_info($error), 'ERR');
        return $attempt <= 3;
    }
}
